package com.useeinfo.oa.open.admin.wuHanCommon;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.func.excel.ExcelExport_New;
import com.useeinfo.framework.sugar.func.lang.DateUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WuHanPointBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WuHanUnitBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanPoint;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.PointSystem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/web/wuhan/point")
public class WuHanPointAction {

    @Autowired
    private WuHanPointBiz wuHanPointBiz;
    @Autowired
    private WuHanUnitBiz wuHanUnitBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getPointListPage")
    public ModelAndView getPointListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/wuHanCommon/point/pointList");

        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getPointListJSON")
    @ResponseBody
    public JSONObject getPointListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                       @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                       @RequestParam(value = "orderBy", required = false) String orderBy,
                                       @RequestParam(value = "searchCode", required = false) String searchCode) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return wuHanPointBiz.findJSONList(queryParam);
    }

    /**
     * 获取编辑页面
     */
    @RequestMapping("/getPointEditPage")
    public ModelAndView getPointEditPage(@RequestParam(value = "pointId", required = false) String pointId) {


        WuHanPoint wuHanPoint = wuHanPointBiz.findModel(StringConverters.ToLong(pointId));
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("pages/wuHanCommon/point/pointEditPart");
        modelAndView.addObject("wuHanPoint", wuHanPoint);
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeWuHanPointEdit")
    @ResponseBody
    public ResultDto executeWuHanPointEdit(WuHanPoint point,
                                           @RequestParam(value = "select_unit[]", required = false) List<String> units,
                                           @RequestParam(value = "select_system[]", required = false) List<String> systems) {
        //查询点位是否重复
        if (wuHanPointBiz.existWuHanPoint(point)) {
            return ResultUtil.error("点位名称为空或不存在，请重新输入");
        }
        if (CollectionUtils.isNotEmpty(units)) {
            point.setWuHanUnit(wuHanUnitBiz.findModel(StringConverters.ToLong(units.get(0))));
        }
        wuHanPointBiz.addOrUpdate(point);
        return ResultUtil.success();
    }

    /**
     * 逻辑删除机构
     */
    @RequestMapping("/logicRemoveWuHanPoint")
    @ResponseBody
    public ResultDto logicRemoveWuHanPoint(@RequestParam(value = "pointId", required = false) String pointIdParam) {
        wuHanPointBiz.deleteLogic(wuHanPointBiz.findModel(StringConverters.ToLong(pointIdParam)));
        return ResultUtil.success();
    }


    /**
     * 导入点位数据
     */
    @ResponseBody
    @RequestMapping(value = "importData")
    public ResultDto importData(MultipartFile file) {
        try {
            return wuHanPointBiz.importData(file);
        } catch (Exception ex) {
            return ResultUtil.error(ex.getMessage());
        }
    }

    /**
     * 导出点位数据
     */
    @RequestMapping(value = "exportData")
    public void exportData(@RequestParam(value = "orderBy", required = false) String orderBy,
                           @RequestParam(value = "searchCode", required = false) String searchCode,
                           HttpServletResponse response) {
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("searchCode", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        List<WuHanPoint> wuHanPointList = wuHanPointBiz.findList(queryParam);
        String fileName = "点位数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        try (ExcelExport_New ee = new ExcelExport_New("点位数据", WuHanPoint.class)) {
            ee.setDataList(wuHanPointList).write(response, fileName);
        }
    }


    /**
     * 获取点位系统选择数据
     *
     * @param q
     * @return
     */
    @RequestMapping("/getPointSystemSelect")
    @ResponseBody
    public Set<SelectVO> getPointSystemSelect(@RequestParam(value = "q", required = false) String q) {
        Set<SelectVO> selectVOS = new HashSet<>();
        PointSystem[] pointSystems = PointSystem.values();
        for (PointSystem pointSystem : pointSystems) {
            selectVOS.add(new SelectVO(pointSystem.getName(), pointSystem.getCode()));
        }
        return selectVOS;
    }

}
